local gfx = require "libs/gfx"
local sfx = require "libs/sfx"
local net = require "libs/net"
local phy = require "libs/phy"

sfx.open()

local gl = gfx.gl
local C = ffi.C

gfx.set_mode(1024, 768, false)

--gl.glEnable(gl.GL_TEXTURE_2D)
gl.glDisable(gl.GL_TEXTURE_2D)

-- Load texture

local accelerated_image = gfx.acc_image(gfx.load_image("F:/test.png"))
local ball = gfx.acc_image(gfx.load_image("F:/face.png"))
--local font = gfx.load_font("c:/windows/fonts/arialbd.ttf")
local font = gfx.load_font("c:/windows/fonts/impact.ttf")

local r = 21
local space = phy.create_space()
local body = phy.create_body(space, 10, r)
local body2 = phy.create_body(space, 10, r)
body.p.x = 100
body.p.y = 100
body.v.x = 10
body.v.y = 10
body2.p.x = 100
body2.p.y = 200
body2.v.y = -100
local shape = phy.create_circle(space, body, r, phy.vect(-20, -20))
local shape2 = phy.create_circle(space, body2, r, phy.vect(-20, 20))
shape.shape.density = 0.1
shape.shape.u = 1
shape2.shape.density = 0.2
shape2.shape.u = 1
phy.balance_body(body)
phy.balance_body(body2)

while gfx.update() do
	if gfx.window.buttonState[ffi.C.kbA] ~= 0 then
		break
	elseif gfx.window.buttonState[ffi.C.kbP] ~= 0 then
		gfx.window.buttonState[ffi.C.kbP] = 0
		sfx.play()
	end

	gfx.clear()
		
	gfx.geom_images()
	gfx.image(20, 20, accelerated_image)

	gfx.rot_image(body.p.x, body.p.y, body.a, 1, ball)
	gfx.rot_image(body2.p.x, body2.p.y, body.a, 1, ball)

	--gfx.geom_color(1, 1, 1, 1)
	--font:draw(20, 400, tostring(sfx.calls()))
	--font:draw(20, 500, tostring(sfx.underflows()))
	font:draw(20, 500, tostring(sfx.pos()))

	gfx.geom_color(1, 1, 1, 1)
	gfx.geom_lines()
	gfx.line(20, 20, 15, 15)

	gfx.line(20 + 789, 20 + 228, 25 + 789, 25 + 228)

	--789, 228

	gfx.end_drawing()

	phy.step(space)
	C.Sleep(10)
end

gfx.close()
sfx.close()

print("ok")